<?php 
namespace Admin\Controller;


//公共的控制器类
class FriendController extends AdminController
{
	public function index()
	{
		$friend = D('friend');
		$search = I('get.name');
		if(empty($search)) {
			$count = $friend->order('id desc')->count();
			// var_dump($count);exit;
			$Page = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$show = $Page->show();// 分页显示输出

			$list = $friend->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
			$this->assign('list',$list);
			$this->assign('page',$show);
			$this->display();
			exit;
		}

		$count = $friend->order('id desc')->where('name like "%'.$search.'%"')->count();
		// var_dump($count);exit;
		$Page = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$map['name'] = $_GET['name'];
		// var_dump($map);
		$Page->parameter=$map;
		$show = $Page->show();// 分页显示输出

		$list = $friend->order('id desc')->where('name like "%'.$search.'%"')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();

	}
	//执行添加页面
	public function add()
	{
		$this->display();
	}
	//执行添加操作
	public function doadd()
	{
		$friend = D('friend');
		// var_dump($friend->create());exit;
		if(!$friend->create()){
			$this->error($friend->getError());
			exit;
		}

		if($friend->add() > 0){
			$this->success("添加成功！",U('friend/index'));
		}else{
			$this->error("添加失败！");
		}
	}

	public function doajax()
	{
		$id=I('post.id');
		$friend = D('friend');
		$status = $friend->field('status')->where('id='.$id)->find();
		if($status['status']==0) {
			$status['status']=1;
			$friend->where('id='.$id)->save($status);
			 echo "未营业";
		} else {
			$status['status']=0;
			$friend->where('id='.$id)->save($status);
			echo "营业中";
		}
	}

	public function edit()
	{
		$friend = D('friend');
		$id=I('get.id');
		// var_dump($id);exit;
		$list = $friend->where('id='.$id)->find();
		// var_dump($list);exit;
		$this->assign('list',$list);
		$this->display('friend/edit');
	}
	public function save()
	{
		$friend = D('friend');

		// var_dump($friend->create());exit;
		//初始化
		if(!$friend->create()){
			$this->error($friend->getError());
			exit;
		}
		// var_dump($friend->fetchSql()->save());exit;
		//执行修改 
		if($friend->save() >= 0){
			$this->success("修改成功！",U('friend/index'));
		}else{
			$this->error("修改失败");
		}
	}

	//删除操作
	public function del(){
		$friend = D('friend');
		
		if($friend->delete($_GET['id'])>0){
			$this->success("删除成功！",U('friend/index'));
		}else{
			$this->error("删除失败");
		}
	}
}